<?php
class MediaController extends AdminAppController{
    
    function index()
    {
        $this->loadModel('Admin.Media');
        $media = $this->Media->get_media('all');
        $list_media = array();
        foreach ($media as $art) {
            $temp = array();
            $temp['media_id'] = $art['media']['media_id'];
            $temp['title'] = $art['media_desc']['title'];
            $temp['sort_order'] = $art['media']['sort_order'];
            $temp['status'] = $art['media']['status'];
            $temp['image'] = $art['image']['name'];
            $temp['type'] = $art['media']['type'];
            $list_media[] = $temp;
        }
        $this->set('list_media', $list_media);
    }
    
    function add()
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        
        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        if($this->request->isPost())
        {
            $this->loadModel('Admin.Media');
            $this->loadModel('Admin.MediaDesc');
            $this->loadModel('Admin.Image');
            $data = array(
                        'category_id' => $_POST['category_id'],
                        'date_added' => date("Y-m-d H:i:s"),
                        'date_modified' => date("Y-m-d H:i:s"),
                        'status' => $_POST['status'],
                        'type' => $this->request->data['Media']['type'],
                        'sort_order' => $this->request->data['Media']['sort_order'],
                        );
            $media_id = $this->Media->add($data);

            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'media_id' => $media_id,
                        'title' => $this->request->data['Media']["title_$lang_code"],
                        'link' => $this->request->data['Media']["link_$lang_code"],
                        'description' => $_POST["description_$lang_code"],
                        'lang_code' => $lang_code,
                        );
                $this->MediaDesc->add($data);
            }
            
            $main_image = $_POST['main_image'];
            $data['main_image'] = $main_image;
            $data['other_image'] = array();
            $data['order_image'] = array();
            $this->Image->add($media_id, 'media', $data);
            
            $this->Session->setFlash(
                                'Thêm mới media thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "media", 
                                  "action" => "index"));
        }
    }
    
    function edit($media_id = "")
    {
        if($media_id == ""){
            $this->redirect(array("controller" => "media", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('media_id', $media_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        $this->loadModel('Admin.Image');
        $this->loadModel('Admin.media');
        $this->loadModel('Admin.mediaDesc');

        $media_info = $this->media->get_media_by_id($media_id, 'all');
        $this->set('media_info', $media_info);

        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        $category = $this->Category->get_all_category('all');
        $list_category = $this->Category->filter_category($category);
        $this->set('list_category', $list_category);
        
        $list_image = $this->Image->get($media_id, 'media');
        $this->set('list_image', $list_image);

        if($this->request->isPost())
        {
            $data = array(
                        'category_id' => $_POST['category_id'],
                        'date_modified' => "'".date("Y-m-d H:i:s")."'",
                        'status' => $_POST['status'],
                        'type' => "'".$this->request->data['Media']['type']."'",
                        'sort_order' => "'".$this->request->data['Media']['sort_order']."'",
                        );
            $this->media->edit($media_id, $data);
            
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data['Media']["title_$lang_code"]."'",
                        'link' => "'".$this->request->data['Media']["link_$lang_code"]."'",
                        'description' => "'".$_POST["description_$lang_code"]."'"
                        );
                $this->mediaDesc->edit($media_id, $lang_code, $data);
            }
            
            
            $main_image = $_POST['main_image'];
            $data['main_image'] = $main_image;
            $data['other_image'] = array();
            $data['order_image'] = array();
            $this->Image->edit($media_id, 'media', $data);
            
            $this->Session->setFlash(
                                'Sủa media thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "media", 
                                  "action" => "index"));
            
        }
    }
}